using System;
using System.Collections.Generic;
using System.Text;

namespace PersistenceObject.Framework.Expressions
{
    public class DateTimeField : FieldAdapter
    {
        public DateTimeField(string name) : base(name)
        {
        }

        public DateTimeField(string name, FieldAdapter basefield) : base(name, basefield)
        {
        }

        public DateTimeField As(string name)
        {
            return new DateTimeField(string.Format("{0} {1}", this.Name, name), this);
        }

        public DateTimeField At(Table table)
        {
            String[] tempArr = this.Name.Split('.');
            if (tempArr.Length > 1)
            {
                this.name = tempArr[1];
            }
            string alias = table.Alias;
            if (string.IsNullOrEmpty(alias))
            {
                alias = table.Name;
            }
            return new DateTimeField(string.Format("{0}.{1}", alias, this.Name), this);
        }

        public Expression Between(DateTime lvalue, DateTime rvalue)
        {
            return Expressions.Between(this.Name, lvalue, rvalue);
        }

        public Expression In(params DateTime[] value)
        {
            return Expressions.In(this.Name, value);
        }

        public Expression In(Table table, Expression exp)
        {
            return this.In(table, this, exp);
        }

        public Expression In(Table table, DateTimeField field, Expression exp)
        {
            Expression expression = new InTableFieldExpression(this.Name, table.Name, field.Name);
            expression.Add(new IExpression[] { exp });
            return expression;
        }

        public Expression NotBetween(DateTime lvalue, DateTime rvalue)
        {
            return Expressions.Between(this.Name, lvalue, rvalue, true);
        }

        public Expression NotIn(params DateTime[] value)
        {
            return Expressions.In(this.Name, value, true);
        }

        public Expression NotIn(Table table, Expression exp)
        {
            return this.NotIn(table, this, exp);
        }

        public Expression NotIn(Table table, DateTimeField field, Expression exp)
        {
            InTableFieldExpression expression = new InTableFieldExpression(base.Name, table.Name, field.Name);
            expression.Add(new IExpression[] { exp });
            expression.IsNotIn = true;
            return expression;
        }

        public static DateTimeField operator +(DateTimeField field, DateTimeField field1)
        {
            return new DateTimeField(string.Format("{0}+{1}", field.Name, field1.Name), field);
        }

        public static Expression operator ==(DateTimeField field, DateTime value)
        {
            return Expressions.Equals(field.Name, value);
        }

        public static Expression operator >(DateTimeField field, DateTime value)
        {
            return Expressions.GreaterThan(field.Name, value);
        }

        public static Expression operator >=(DateTimeField field, DateTime value)
        {
            return Expressions.GreaterThanOrEquals(field.Name, value);
        }

        public static Expression operator !=(DateTimeField field, DateTime value)
        {
            return Expressions.NotEquals(field.Name, value);
        }

        public static Expression operator <(DateTimeField field, DateTime value)
        {
            return Expressions.LessThan(field.Name, value);
        }

        public static Expression operator <=(DateTimeField field, DateTime value)
        {
            return Expressions.LessThanOrEquals(field.Name, value);
        }

        public static DateTimeField operator -(DateTimeField field, DateTimeField field1)
        {
            return new DateTimeField(string.Format("{0}-{1}", field.Name, field1.Name), field);
        }

        public FieldValue Set(DateTime value)
        {
            return new FieldValue(this.Name, value);
        }
    }
}

